#include "EWAPrjTrack.hpp"

namespace Eyer
{
    EWAPrjTrack::EWAPrjTrack()
    {
        trackId = Eyer::EyerRand::RandNumberStr(32);
    }

    EWAPrjTrack::EWAPrjTrack(const EyerString & _trackId)
    {
        trackId = _trackId;
    }

    EWAPrjTrack::~EWAPrjTrack()
    {

    }

    const EyerString & EWAPrjTrack::GetTrackId()
    {
        return trackId;
    }

    int EWAPrjTrack::PushFragmentBack(const EyerSmartPtr<EWAPrjFragment> & fragment)
    {
        fragmentList.push_back(fragment);
        Update();
        return 0;
    }

    int EWAPrjTrack::GetFragmentCount()
    {
        return fragmentList.size();
    }

    EyerSmartPtr<EWAPrjFragment> EWAPrjTrack::GetFragment(int index)
    {
        return fragmentList[index];
    }

    int EWAPrjTrack::RegisterListener(EWAPrjTrackListener * listener)
    {
        listenerList.push_back(listener);
        return 0;
    }

    int EWAPrjTrack::UnregisterListener(EWAPrjTrackListener * listener)
    {
        for(auto it = listenerList.begin(); it != listenerList.end(); ) {
            if(*it == listener) {
                listenerList.erase(it);
            } else {
                it++;
            }
        }
        return 0;
    }

    int EWAPrjTrack::Update()
    {
        for(int i = 0; i < listenerList.size(); i++){
            listenerList[i]->OnFragmentUpdate(this);
        }
        return 0;
    }
}